<template>
  <div>
    <el-form
      :model="form"
      :rules="rules"
      label-width="100px"
      ref="form"
      class="kylin-form"
    >
      <el-form-item :label="$t('system.script_name')" prop="name">
        <el-input
          v-model="form.name"
          autocomplete="off"
          maxlength="20"
        ></el-input>
      </el-form-item>
      <el-form-item :label="$t('system.script_content')" prop="content">
        <el-input
          v-model="form.content"
          autocomplete="off"
          type="textarea"
          :autosize="{ minRows: 5, maxRows: 5 }"
        ></el-input>
      </el-form-item>
    </el-form>
    <div slot="footer" class="dialog-footer">
      <el-button @click="handleCancel">{{ $t("cancel") }}</el-button>
      <el-button class="kylin-button" @click="handleOk">{{
        $t("save")
      }}</el-button>
      <el-button class="kylin-button" @click="handleRun">{{
        $t("run")
      }}</el-button>
    </div>
  </div>
</template>

<script>
import { createScripts, getScript, checkScript } from "@/api/script";
import { successOrError } from "@/utils";
export default {
  props: {
    type: {
      type: String,
      default: "",
    },
    row_id: {
      type: String | Number,
    },
  },
  data() {
    return {
      title: "",
      form: {
        name: "",
        content: "",
        verified: true,
      },
      rules: {
        name: [
          {
            required: true,
            message: this.$t("system.script_name_require"),
            trigger: "change",
          },
        ],
        content: [
          {
            required: true,
            message: this.$t("system.script_content_require"),
            trigger: "change",
          },
        ],
      },
    };
  },
  mounted() {
    if (this.type === "update") {
      getScript({ id: this.row_id }).then((res) => {
        this.form.name = res.data.data.name;
        this.form.content = res.data.data.content;
      });
    }
  },
  methods: {
    handleClose() {
      this.$refs.form.resetFields();
      this.$emit("click");
    },
    handleCancel() {
      this.$refs.form.resetFields();
      this.$emit("click");
    },
    handleOk() {
      this.$refs.form.validate((valid) => {
        if (valid) {
          checkScript({ content: this.form.content }).then((res) => {
            let bol = res.data.data;
            if (bol) {
              createScripts(this.form).then((res) => {
                successOrError.call(this, res);
              });
            } else {
              this.$confirm("该脚本存在危险命令，请确认是否要操作？", "提示", {
                confirmButtonText: "确定",
                cancelButtonText: "取消",
                type: "warning",
              }).then(() => {
                createScripts(this.form).then((res) => {
                  successOrError.call(this, res);
                });
              });
            }
          });
        }
      });
    },
    handleRun() {
      this.$refs.form.validate((valid) => {
        if (valid) {
          this.$emit("click", { ...this.form });
          this.$refs.form.resetFields();
        }
      });
    },
  },
};
</script>